package com.liu.mongodb.demo.response;

import org.springframework.util.StringUtils;

import java.io.Serializable;

public class BasicResponse<T> implements Serializable {

    protected String code;
    protected String msg;
    protected T data;

    public BasicResponse OK(){
        BasicResponse<String> basicResponse = new BasicResponse("1","success",null);
        return basicResponse;
    }

    public static BasicResponse<String> OK(String message){
        BasicResponse<String> basicResponse = new BasicResponse("1",message,null);
        return basicResponse;
    }

    public static BasicResponse OK(String message,Object obj){
        BasicResponse<String> basicResponse = new BasicResponse("1",message,obj);
        return basicResponse;
    }

    public BasicResponse(String code,String message,T obj){
        this.code = StringUtils.hasLength(code)?code:"";
        this.msg = StringUtils.hasLength(message)?message:"";
        this.data = null != obj?obj:null;
    }

    public static BasicResponse ERROR(String message){
        BasicResponse basicResponse = new BasicResponse("0",message,null);
        return basicResponse;
    }

    public static BasicResponse ERROR(String code,String message){
        BasicResponse basicResponse = new BasicResponse(code,message,null);
        return basicResponse;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
